package cn.stylefeng.guns.modular.system.common;

import cn.stylefeng.guns.animals.modular.MysqlDump.IMysqlDumpService;
import cn.stylefeng.guns.core.log.LogManager;
import cn.stylefeng.guns.core.log.factory.LogTaskFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author <a mailto:dongdongliang13@hotmail.com>zzt002</a>
 * @date 2020/9/23 17:31 GMT+8
 * @description
 */
@Component
@Slf4j
public class MysqlSchedule {

    @Autowired
    private IMysqlDumpService mysqlDumpService;

//    @Scheduled(cron = "0 0 0 * * ?")
    public void task() {
        log.info("--------------mysql备份开始--------------");
        long startMills = System.currentTimeMillis();
        int ret = -1;
        String errorMessage = null;
        try {
            ret = mysqlDumpService.backup();
        } catch (Exception e) {
            e.printStackTrace();
            errorMessage = e.getMessage();
        }
        long endMills = System.currentTimeMillis();
        String message = ret == 0 ? "成功" : "失败:" + errorMessage;
        log.info("--------------mysql备份结束,执行{},耗时：{}ms--------------", message, endMills - startMills);
        LogManager.me().executeLog(LogTaskFactory.bussinessLog(null, "数据库备份", "MysqlSchedule", "task", message));
    }

    @PostConstruct
    public void startTest() {
        // task();
    }
}
